package com.mmlc.meimei.lichengkongjian;

import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.mmlc.inter.InterActivity;
import com.mmlc.network.filter.WgListRequestFilter;
import com.mmlc.network.responsebean.WgListResponseBean;
import com.mmlc.utils.DataUtils;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.XUtils;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;


public class WGActivity extends InterActivity implements View.OnClickListener {


    ImageView iv_set, iv_back;
    TextView tv_title;
    PullToRefreshListView pullToRefreshListView;
    List<WgListResponseBean.DataBean> datas = new ArrayList<>();
    CommonAdapter<WgListResponseBean.DataBean> adapter;

    @Override
    protected int initContentView() {
        return R.layout.activity_wp;
    }

    @Override
    protected void initView() {
        iv_set = (ImageView) findViewById(R.id.iv_set);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("我购");
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        iv_set.setVisibility(View.GONE);
        pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        pullToRefreshListView.setAdapter(adapter = new CommonAdapter<WgListResponseBean.DataBean>(this, R.layout.content_wg, datas) {
            @Override
            protected void convert(ViewHolder viewHolder, final WgListResponseBean.DataBean item, int position) {
                viewHolder.setText(R.id.ys_s, item.getUbname());
                viewHolder.setText(R.id.cm_s, item.getUsize());
                viewHolder.setText(R.id.time, item.getAdd_time());
                viewHolder.setText(R.id.status, item.getIs_apply());
                viewHolder.setText(R.id.pj_s, item.getHope_price());
                viewHolder.setText(R.id.sl_s, item.getUnum());
                ImageView imageView = viewHolder.getView(R.id.imageView);
                XUtils.image(imageView, item.getAlbum().get(0).getImg());
                if(item.getIs_apply().equals("未回复")){
                    viewHolder.setVisible(R.id.msg,false);
                }else{
                    viewHolder.setVisible(R.id.msg,true);
                    viewHolder.setText(R.id.msg,item.getSysnote());
                }
                viewHolder.setOnClickListener(R.id.imageView, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ArrayList<String> list=new ArrayList<String>();
                        for(WgListResponseBean.DataBean.AlbumBean d:item.getAlbum()){
                            list.add(d.getImg());
                        }
                        DataUtils.toJump(context,0,list);
                    }
                });
            }
        });
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        processData();
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) throws InterruptedException {
                page = 0;
                datas.clear();
                pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
                processData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                processData();
            }
        });
    }

    int page = 0;

    private void processData() {
        WgListRequestFilter wpRequestFilter = new WgListRequestFilter(this);
        wpRequestFilter.requestBean.pageNow = page + "";
        wpRequestFilter.requestBean.pageSize = "" + 20;
        wpRequestFilter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<WgListResponseBean>() {

            @Override
            public void onSuccess(WgListResponseBean result) {
                if (result.getCode() == 0) {
                    datas.addAll(result.getData());
                    adapter.notifyDataSetChanged();
                    pullToRefreshListView.onRefreshComplete();
                    if(result.getData().size()==0){
                        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                    }
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }
}
